package com.superb.common.doc.config;

import com.github.xiaoymin.knife4j.solon.extension.OpenApiExtensionResolver;
import com.superb.common.doc.config.properties.Knife4jProperties;
import com.superb.common.utils.HeadersUtils;
import io.swagger.models.Scheme;
import io.swagger.models.parameters.HeaderParameter;
import org.noear.solon.Solon;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.noear.solon.docs.DocDocket;
import org.noear.solon.docs.models.ApiContact;
import org.noear.solon.docs.models.ApiInfo;
import org.noear.solon.docs.models.ApiResource;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: ajie
 * @CreateTime: 2024-07-25 16:05
 */
@Configuration
public class Knife4jConfig {

    @Inject
    private OpenApiExtensionResolver openApiExtensionResolver;

    @Inject(value = "${knife4j.custom}", autoRefreshed = true)
    private Knife4jProperties properties;
    @Inject("${solon.app.name}")
    private String name;
    /**
     * 丰富点的
     * 不注册gateway
     */
    @Bean("adminApi")
    @Condition(onProperty="${solon.app.name} != superb-gateway")
    public DocDocket adminApi() {
        System.err.println("gateway:" + "," + name);
        return new DocDocket()
                .groupName(properties.getGroupName())
                .vendorExtensions(openApiExtensionResolver.buildExtensions())
                .info(new ApiInfo().title(properties.getTitle())
                        .description(properties.getDescription())
                        .contact(new ApiContact().name(properties.getAuthor())
                                .email(properties.getEmail()))
                        .version(properties.getVersion()))
                .schemes(Scheme.HTTP.toValue(), Scheme.HTTPS.toValue())
                .globalParams(new HeaderParameter().name(HeadersUtils.tenantId).description("租户id").required(true))
                .globalParams(new HeaderParameter().name(HeadersUtils.clientId).description("客户端id").required(true))
                .globalParams(new HeaderParameter().name(HeadersUtils.organId).description("所属部门id"))
                .globalParams(new HeaderParameter().name(HeadersUtils.token).description("登录凭证"))
                .globalParams(new HeaderParameter().name("version").description("灰度版本"))
                .apis(new ApiResource()); //所有存在Mapping注解的方法
    }

}
